package com.party.admin.biz.quartz.factory;

import com.alibaba.druid.support.json.JSONUtils;
import com.party.admin.biz.quartz.impl.SchsduleService;
import com.party.common.constant.Constant;
import com.party.common.reflect.ReflectUtils;
import com.party.common.spring.SpringUtils;
import com.party.common.utils.StringUtils;
import com.party.core.model.notify.Event;
import com.party.core.model.quartz.QuartzDataMap;
import com.party.core.service.quartz.impl.QuartzDataMapService;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.util.List;

/**
 * 异步任务工厂类
 * Created by wei.li
 *
 * @date 2017/4/12 0012
 * @time 10:21
 */
public class AsyncJobFactory extends QuartzJobBean {

    private static final Logger logger = LoggerFactory.getLogger(AsyncJobFactory.class);

    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        try {
            JobDataMap jobDataMap = context.getMergedJobDataMap();
            boolean isBiz = jobDataMap.getBoolean(SchsduleService.IS_BIZ);
            if (!isBiz) {
                // 不是业务, 就走以前的路线
                Event event = (Event) context.getMergedJobDataMap().get(Constant.JOB_PARAM_KEY);
                logger.info("运行的定时任务是{}, 详情{}", event.getName(), event.toString());

                ReflectUtils.invokMethod(event.getClassName(), event.getMethod());
            } else {
                // 是业务, 就走新的程序动态生成定时任务路线
                String triggerName = context.getTrigger().getKey().getName();
                String jobName = context.getJobDetail().getKey().getName();

                String id = triggerName.substring(jobName.concat("_").length());

                Event event = (Event) jobDataMap.get(Constant.JOB_PARAM_KEY);

                String targetId = SchsduleService.PARAMS.concat(SchsduleService.SEND_SHORT_MESSAGE_LINE).concat(id);
                QuartzDataMapService quartzDataMapService = (QuartzDataMapService) SpringUtils.getBean("quartzDataMapService");
                QuartzDataMap quartzDataMap = quartzDataMapService.findByTargetId(targetId);
                String json = quartzDataMap == null ? null : quartzDataMap.getContent();

                Object paramsObject = StringUtils.isEmpty(json) ? null : JSONUtils.parse(json);

                Object[] params;
                if (null == paramsObject) {
                    params = new Object[]{id};
                } else {
                    List<Object> list = (List<Object>) paramsObject;
                    params = new Object[list.size() + 1];
                    for (int i = 0, j = list.size(); i < j; i++) {
                        params[i] = list.get(i);
                    }
                    params[params.length - 1] = id;
                }

                logger.info("运行的定时任务是{}, 详情{}", event.getName(), event.toString());
                ReflectUtils.invokeMethod(event.getClassName(), event.getMethod(), params);
            }
        } catch (Exception e) {
            logger.error("定时任务运行异常", e);
        }
    }
}
